<?php

class player extends MY_Controller{
	private $status = true;
	
	function __construct(){
		parent::__construct(false);
	}
	
	public function _remap($method){
		//informacion del usaurio logeado
		$this->params['oauth_token'] = Sys::getTokenVal();
		if($this->params['oauth_token'] != FALSE){
			$this->load->model('user_model');
			$info = $this->user_model->getInfoUser($this->params);
			$this->params['usr_info'] = $info['user'];
		}
		
		if($method=="index"){
	    	$this->sin_plugin();
		}else
	    	$this->{$method}();
	}
	
	
	/**
	 * Carga el reproductor con la fuente
	 */
	protected function view(){
		//CSS y JS Base
		$this->carabiner->css(array(
			array('dev/style.css', 'screen', 'style.css')
		));
		$this->carabiner->js(array(
			array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
			array('dev/general-play.js', 'screen', 'general-play.js')
		));
		
		$this->load->view("interface/skin/player/view", $this->params);
	}
	
	/**
	 * Muestra el contador mientras carga la fuente
	 */
	protected function play(){
		$this->carabiner->css(array(
			array('dev/style.css', 'screen', 'style.css')
		));
		$this->carabiner->js(array(
			array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
			array('dev/general-play.js', 'screen', 'general-play.js')
		));
		
		$this->load->view("interface/skin/player/play", $this->params);
	}
	
	/**
	 * Mostrar las fuetes de donde se puede reproducir el video
	 */
	protected function video(){
		if(isset($this->params['t'])){ //Capitulo de serie
			$this->params['episode'] = $this->params['video'];
			$this->load->model('series_model');
		
			$episode = $this->series_model->getEpisode($this->params);
			if(is_array($episode)){
				$this->params['film'] = $episode;
			}
		}else{ //pelicula
			$this->params['t'] = 'm';
			$this->params['id_film'] = $this->params['video'];
			$this->load->model('films_model');
			
			$film = $this->films_model->getInfoFilm($this->params);
			if(is_array($film[0]))
				$this->params['film'] = $film[0];
		}
		
		$this->load->view("interface/skin/player/video", $this->params);
	}
	
	/**
	 * Mostrar skin para instalar el plugin
	 */
	public function sin_plugin(){
		$this->load->library('user_agent');
		
		$this->params['title']		= lang('v_no_plugins');
		$this->params['browser'] 	= '';
		if($this->agent->is_browser('Chrome')){
			$this->params['browser'] 	= 'chrome';
			$this->params['title']		= lang('v_no_plugins_ds', 'Chrome');
		}elseif($this->agent->is_browser('Firefox')){
			$this->params['browser'] 	= 'firefox';
			$this->params['title']		= lang('v_no_plugins_ds', 'Firefox');
		}
		
		$this->load->view("interface/skin/player/sin_plugin", $this->params);
	}
	
	/**
	 * Descarga el plugin de firefox
	 */
	public function plugin_firefox(){
		$filename = BASEPATH.'plugins/pelimaniaco.xpi';
		header("Content-Disposition: filename=pelimaniaco.xpi");
		header('Content-type: application/x-xpinstall');
		header('Content-Length: ' . filesize($filename));
		readfile($filename);
	}
	
	
	function migrar_pelis(){
		$calidad = array(
			'ids' => array(1, 48, 49, 50, 51, 3, 2),
			'nombres' => array('DVDRip','R5','DVDScreener','HDRip','BRRip 480p','BRRip 720p','BRRip')
		);
		$res = $this->db
			->select("id_pelicula, id_usuarios AS id_usuario, idioma, subtitulos, formato, calidad, duracion, tamanio")
			->from('peliculas')
			->order_by('id_pelicula', 'asc')
			
		->get();
		
		$id_peli = 0;
		$cali_sel = 1;
		foreach($res->result_array() as $keyr => $datos){
			$cali_sel = 1;
			foreach($calidad['nombres'] as $key => $cali){
				if(preg_match("/".$cali."/i", $datos['calidad'])){
					$cali_sel = $key;
					break;
				}
			}
			
			$id_peli = $datos['id_pelicula'];
			$datos['calidad'] = $calidad['ids'][$cali_sel];
			unset($datos['id_pelicula']);
			$this->db->update('peliculas_fuentes', $datos, 'id_pelicula = '.$id_peli);
			echo $id_peli." => Ok<br>";
		}
	}
	
	function migrar_caps(){
		$calidad = array(
			'ids' => array(1, 48, 49, 50, 51, 3, 2),
			'nombres' => array('DVDRip','R5','DVDScreener','HDRip','BRRip 480p','BRRip 720p','BRRip')
		);
		$res = $this->db
			->select("id_capitulo, id_usuario AS id_usuario, idioma, subtitulos, formato, calidad, duracion, tamanio")
			->from('series_capitulos')
			->where('status = 1')
			->order_by('id_capitulo', 'asc')
			
		->get();
		
		$id_peli = 0;
		$cali_sel = 1;
		foreach($res->result_array() as $keyr => $datos){
			$cali_sel = 3;
			/*foreach($calidad['nombres'] as $key => $cali){
				if(preg_match("/".$cali."/i", $datos['calidad'])){
					$cali_sel = $key;
					break;
				}
			}*/
			
			$id_peli = $datos['id_capitulo'];
			$datos['calidad'] = $calidad['ids'][$cali_sel];
			unset($datos['id_capitulo']);
			$this->db->update('series_capitulos_fuentes', $datos, 'id_capitulo = '.$id_peli);
			echo $id_peli." => Ok<br>";
		}
	}
	
	
	
	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    protected function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
}
?>
